package com.my.xinyidaixi.Fragment;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.my.xinyidaixi.Activity.complete_order;
import com.my.xinyidaixi.Activity.dingdanxiangqing;
import com.my.xinyidaixi.Activity.login_activity;
import com.my.xinyidaixi.Adapter.ListViewOrder_undone_Adapter;
import com.my.xinyidaixi.DataBase.Bean.Dingdan;
import com.my.xinyidaixi.DataBase.Model.Over_UserModer;
import com.my.xinyidaixi.DataBase.Model.UserModel;
import com.my.xinyidaixi.DataBase.presenter.Userpresenter;
import com.my.xinyidaixi.Globe.MyApplication;
import com.my.xinyidaixi.HttpVolley.Volley_core;
import com.my.xinyidaixi.R;

import java.util.ArrayList;

/**
 * Created by zw on 2017/6/1.
 */
public class Order_Fragment extends Fragment {

    private Context context= MyApplication.getInstance();
    private View fragment_2;
    private Intent intent;
    private Userpresenter presenter;
    private TextView login_layout_login;
    private UserModel model=new Over_UserModer();
    private ListView undobe_list;
    private ArrayList<Dingdan> dingdenlist=new ArrayList<>();
    private ListViewOrder_undone_Adapter undoneAdapter;
    private SwipeRefreshLayout swiperefresh;
    private Handler mhandler=new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            if(msg.what==1)
            {
                dingdenlist.clear();
                dingdenlist.addAll(model.getorderinfo());
                undoneAdapter.notifyDataSetChanged();
               // ToastUtil.shortToast(context,"订单信息更新成功，再刷新一下");
                swiperefresh.setRefreshing(false);
            }
            if(msg.what==2)
            {
                dingdenlist.clear();
                dingdenlist.addAll(model.getorderinfo());
                undoneAdapter.notifyDataSetChanged();
                //undoneAdapter.notifyDataSetChanged();
                //undobe_list.setAdapter(undoneAdapter);
                //ToastUtil.shortToast(context,"订单信息更新成功，再刷新一下");
                swiperefresh.setRefreshing(false);
            }
        }
    };
    private TextView complete_button;

   /* @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden)
        {
        }
    }*/

    @Override
    public void onResume() {
        super.onResume();
        if(model.count()>0)
        {
            login_layout_login.setVisibility(View.GONE);
            undobe_list.setVisibility(View.VISIBLE);
            swiperefresh.setVisibility(View.VISIBLE);
            complete_button.setEnabled(true);
        }
        else
        {
            login_layout_login.setVisibility(View.VISIBLE);
            undobe_list.setVisibility(View.INVISIBLE);
            swiperefresh.setVisibility(View.INVISIBLE);
            complete_button.setEnabled(false);
            dingdenlist.clear();
        }
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        fragment_2 = inflater.inflate(R.layout.order,container,false);
        initdata();
        return fragment_2;
    }

    public void initdata()
    {
        login_layout_login = (TextView) fragment_2.findViewById(R.id.order_login);
        login_layout_login.setOnClickListener(new button());
        undobe_list = (ListView) fragment_2.findViewById(R.id.order_undone_list);
        complete_button = (TextView) fragment_2.findViewById(R.id.order_complete_button);
        complete_button.setOnClickListener(new button());

        dingdenlist=model.getorderinfo();
        undoneAdapter=new ListViewOrder_undone_Adapter(context,dingdenlist);
        undobe_list.setAdapter(undoneAdapter);

        swiperefresh = (SwipeRefreshLayout) fragment_2.findViewById(R.id.swiperefresh);
        swiperefresh.setColorSchemeColors(0xFF00DBF5);//设置加载显示颜色
        swiperefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Thread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        try
                        {
                            Volley_core volley=new Volley_core();
                            volley.cleaningorders(model.getUser(),0); //翻页刷新
                            Thread.sleep(2000);
                            mhandler.sendEmptyMessage(2);
                        }
                        catch (InterruptedException e)
                        {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
        //开启新的线程刷新列表
        undoneAdapter.setOnclicklistenerpositon(new ListViewOrder_undone_Adapter.Onclicklistenerlist() {
            @Override
            public void Onclicklistenerposition(int position) {
                Intent intent=new Intent();
                intent.putExtra("id",dingdenlist.get(position).getId());
                intent.setClass(context,dingdanxiangqing.class);
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.ani_right_get_into,R.anim.ani_left_sign_out);
            }
        });

    }

    //通过回调函数来改变UI

    public class button implements View.OnClickListener
    {

        @Override
        public void onClick(View v)
        {
            switch (v.getId())
            {
                case R.id.order_login:
                    intent=new Intent();
                    intent.setClass(context,login_activity.class);
                    startActivityForResult(intent,1);
                    getActivity().overridePendingTransition(R.anim.ani_right_get_into,R.anim.ani_left_sign_out); //activity切换效果
                    break;

                case R.id.order_complete_button:
                    intent=new Intent();
                    intent.setClass(context,complete_order.class);
                    startActivity(intent);
                    getActivity().overridePendingTransition(R.anim.ani_right_get_into,R.anim.ani_left_sign_out);
                    break;
            }
        }
    }

    //实现自动刷新
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if(requestCode==resultCode)
        {
            Volley_core volley=new Volley_core();
            volley.cleaningorders(model.getUser(),0); //翻页刷新
            swiperefresh.setRefreshing(true);
            swiperefresh.postDelayed(new Runnable()
            {
                @Override
                public void run()
                {
                    mhandler.sendEmptyMessage(1);
                }
            },3000);
        }

    }


}
